/**
 * Created by Administrator on 2017/8/29 0029.
 */
operateFormatter = function (value, row, index) {
    if (row.picPath == null) {
        return [
            '<a class="removes" href="javascript:void(0)" style="color: red">删除</a>'
        ].join('');
    } else {
        return [
            '<a class="replace" href="javascript:void(0)" >更换图片</a>' +
            '&nbsp;&nbsp;' +
            '<a class="removes" href="javascript:void(0)" style="color: red">删除</a>'
        ].join('');
    }
};
window.operateEvents = {
    'click .removes': function (e, value, row) {
        comment.showOperationTips("确定删除吗？", function () {
            $.post($().AbsUrlParse('/web/carousel/updateState'), {
                id: row.id, link: row.link
            }, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("删除成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    },
    'click .replace': function (e, value, row) {
        showModel("replace-div", function () {
            $("#idUpdate").val(row.id);
            $("#image").attr("src", File_Download_Url + "/" + row.picPath);
            $("#image").attr("img-path", row.picPath);
        })
    }
};

storePic = function (value, row, index) {
    var img = row.picPath;
    var imgUrl = "";
    if (img != null && img != '') {
        imgUrl = File_Download_Url + img;
        console.log("imgUrl=" + imgUrl);
        return [
            '<image src=' + imgUrl + ' style="width:120px;height:80px" onclick="enlargeImage(this)"/>'
        ].join('');
    } else {
        return [
            '<image src=' + $().AbsUrlParse("/static/images/no.png") + ' style="width:120px;height:80px" onclick="enlargeImage(this)"/>'
        ].join('');
    }
};

function queryAdvName() {
    var name = $("#name").val();
    var city = $("#city").val();
    var type = $("#type").val();
    var url = $().AbsUrlParse("/web/carousel/queryCarousel?name=" + name + "&cityId=" + city + "&type=" + type);
    var table = $("#table");
    table.bootstrapTable("refresh", {url: url});
}

var getCityIds = function () {
    $.post($().AbsUrlParse("/web/type/queryByCityId"), {deptPath: 3}, function (id) {
        var select = $("#city");
        select.empty();
        select.append('<option value="-1">-城市选择-</option>');
        if (id.res.code == 40000) {
            var allData = id.res.data;
            for (var i = 0; i < allData.length; i++) {
                select.append('<option value="' + allData[i].id + '">' + allData[i].name + '</option>');
            }
        }
    });
};
getCityIds();


orderNum = function (value, row, index) {
    if (row.order_num == null) {
        return [
            '<a class="updateNum" href="javascript:void(0)" >' + 0 + '</a>'
        ].join()
    } else {
        return [
            '<a class="updateNum" href="javascript:void(0)" >' + row.order_num + '</a>'
        ].join()
    }
};

window.operateOrderNum = {
    'click .updateNum': function (e, value, row) {
        showModel("updateNum-div", function () {
            $("#id").val(row.id);
            $("#orderNum").val(row.order_num);
        })
    }
};

/**
 * 修改按钮事件
 */
bindClick("button[data-event=submit-updateNum]", function (tag, typeId) {

    var num = $("#orderNum").val();
    var id = $("#id").val();
    $.post($().AbsUrlParse('/web/management/updateOrderNum'), {id: id, orderNum: num}, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('修改成功！');
            //关闭model方法
            closeModel('updateNum-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});


/**
 * 修改按钮事件
 */
bindClick("button[data-event=submit-replacePicture]", function () {
    var imag = $("#image").attr("img-path");
    if (imag == null || imag == "") {
        hint("请上传图片");
        return;
    }
    $("#pic").val(imag);

    var form = $("#updateReplaceFrom");
    var v = form.valid();
    if (v == false) {
        return false;
    }
    var param = form.formToArray();
    $.post($().AbsUrlParse('/web/carousel/replacePicture'), param, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('设置成功');
            //关闭model方法
            closeModel('replace-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});

function enlargeImage(value) {
    console.log("enlargeImage=" + value.src)
    showModel("detailImage-div", function () {
        var imgs = '<image src=' + value.src + ' style="height:500px" />';
        console.log(imgs)
        $("#detailImage").html(imgs);
    })
}